package com.agileboot.medicine.controller;

import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.medicine.command.MedicineCommand;
import com.agileboot.domain.medicine.db.Medicine;
import com.agileboot.domain.medicine.db.MedicineService;
import com.agileboot.domain.medicine.dto.MedicineDTO;
import com.agileboot.domain.medicine.query.MedicineQuery;
import com.agileboot.domain.medicine.query.MedicineSearchQuery;
import com.agileboot.domain.medicine.query.MyMedicineQuery;
import com.agileboot.domain.medicine.vo.MedicineInfoVO;
import com.agileboot.domain.medicine.vo.MedicineVO;
import com.agileboot.domain.system.user.dto.UserDTO;
import com.agileboot.domain.system.user.dto.UserRanking;
import com.sun.jna.platform.unix.solaris.LibKstat;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * (Medicine)表控制层
 *
 * @author makejava
 * @since 2023-12-12 11:12:52
 */
@RestController
@RequestMapping("/medicine")
public class MedicineUserController {
    @Autowired
    private MedicineService medicineService;
    /**
     * 首页推荐模块
     */
    @Operation(summary = "首页推荐草药模块")
    @GetMapping("/suggest")
    public ResponseDTO<List<MedicineInfoVO>> getSuggestMedicineList() throws IOException {
        List<MedicineInfoVO> medicineVOPage = medicineService.getSuggestMedicineList();
        return ResponseDTO.ok(medicineVOPage);
    }

    /**
     * 用户上传草药
     */
    @Operation(summary = "用户上传草药")
    @PostMapping("/upload")
    public ResponseDTO uploadMedicine(@RequestBody MedicineCommand medicineCommand) throws IOException {
        medicineService.uploadMedicine(medicineCommand);
        return ResponseDTO.ok();
    }

    /**
     * 搜索模块
     */
    @Operation(summary = "搜索模块")
    @GetMapping("/search")
    public ResponseDTO<MedicineVO> searchMedicineList(MedicineSearchQuery medicineSearchQuery) throws IOException {
        MedicineVO medicineVO = medicineService.searchMedicineList(medicineSearchQuery);
        return ResponseDTO.ok(medicineVO);
    }

    /**
     * 搜索模块
     */
    @Operation(summary = "搜索详情")
    @GetMapping("/detail")
    public ResponseDTO<MedicineInfoVO> getMedicineDetail(MedicineDTO medicineDTO) throws IOException {
        MedicineInfoVO medicineDetail = medicineService.getMedicineDetail(medicineDTO);
        return ResponseDTO.ok(medicineDetail);
    }

    /**
     * 搜索模块
     */
    @Operation(summary = "通过id搜索详情")
    @GetMapping("/detail/info")
    public ResponseDTO<MedicineInfoVO> getMedicineDetailById(String id) throws IOException {
        MedicineInfoVO medicineDetail = medicineService.getMedicineDetailById(id);
        return ResponseDTO.ok(medicineDetail);
    }


    @Operation(summary = "草药列表导入")
    @PostMapping("/excel")
    public ResponseDTO<Void> importMedicineByExcel(MultipartFile file) throws IOException {
        List<MedicineCommand> commands = CustomExcelUtil.readFromRequest(MedicineCommand.class, file);
        medicineService.addMedicine(commands);
        return ResponseDTO.ok();
    }

    @Operation(summary = "用户上传的草药分页列表")
    @PostMapping("/my-upload")
    public ResponseDTO<PageDTO<Medicine>> myUploadMedicineList(MyMedicineQuery myMedicineQuery) throws IOException {
        PageDTO<Medicine> medicineDTOS = medicineService.myUploadMedicineList(myMedicineQuery);
        return ResponseDTO.ok(medicineDTOS);
    }
    @Operation(summary = "用户通过草药id删除自己上传的草药")
    @GetMapping("/del")
    public ResponseDTO delMyMedicine(String medicineId) throws IOException {
        return ResponseDTO.ok(medicineService.delMyMedicine(medicineId));
    }

    /**
     * 搜索模块
     */
    @Operation(summary = "排行榜")
    @GetMapping("/ranking")
    public ResponseDTO<List<UserRanking>> rankingUserList() throws IOException {
        List<UserRanking> list = medicineService.rankingUserList();
        return ResponseDTO.ok(list);
    }

}
